package platform

import (
	"BuildAll/internal/utils"
)

// Platform 表示操作系统和架构的组合
type Platform struct {
	OS   string
	Arch string
}

// 定义支持的操作系统和架构
var (
	SupportedOS   = []string{"linux", "darwin", "windows", "freebsd", "openbsd", "netbsd", "dragonfly", "solaris", "plan9", "aix", "android", "ios"}
	SupportedArch = []string{"386", "amd64", "arm", "arm64", "ppc64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "riscv64", "s390x", "wasm"}
)

// GetSupportedOS 返回所有支持的操作系统
func GetSupportedOS() []string {
	return SupportedOS
}

// GetSupportedArch 返回所有支持的架构
func GetSupportedArch() []string {
	return SupportedArch
}

// IsSupportedCombination 检查特定的操作系统和架构组合是否受支持
func IsSupportedCombination(goos, goarch string) bool {
	// wasm只能在js环境下运行
	if goarch == "wasm" && goos != "js" {
		return false
	}

	// 某些特定组合不支持
	unsupportedCombos := map[string][]string{
		// darwin不支持这些架构
		"darwin": {"386", "arm", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64"},
		// windows不支持这些架构
		"windows": {"mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64"},
		// ios只支持arm64，但需要cgo支持
		"ios": {"386", "amd64", "arm", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64", "wasm", "arm64"},
		// android需要cgo支持
		"android": {"386", "amd64", "arm", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64", "wasm"},
		// plan9支持有限
		"plan9": {"arm", "arm64", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64", "wasm"},
		// solaris主要支持amd64
		"solaris": {"386", "arm", "arm64", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64", "wasm"},
		// aix只支持ppc64
		"aix": {"386", "amd64", "arm", "arm64", "mips", "mipsle", "mips64", "mips64le", "s390x", "riscv64", "wasm", "ppc64le"},
		// dragonfly只支持amd64
		"dragonfly": {"386", "arm", "arm64", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "s390x", "riscv64"},
		// freebsd不支持这些架构
		"freebsd": {"ppc64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "s390x"},
		// netbsd不支持这些架构
		"netbsd": {"ppc64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "riscv64", "s390x"},
		// openbsd不支持这些架构
		"openbsd": {"ppc64le", "mips", "mipsle", "mips64", "mips64le", "s390x"},
	}

	// 检查是否在不支持列表中
	if arches, exists := unsupportedCombos[goos]; exists {
		for _, arch := range arches {
			if arch == goarch {
				return false
			}
		}
	}

	return true
}

// GetBuildList 返回所有需要构建的平台组合
func GetBuildList(targetOS, targetArch []string) []Platform {
	var buildList []Platform

	for _, goos := range SupportedOS {
		// 如果指定了目标OS且当前OS不在列表中，跳过
		if len(targetOS) > 0 && !utils.Contains(targetOS, goos) {
			continue
		}

		for _, goarch := range SupportedArch {
			// 如果指定了目标架构且当前架构不在列表中，跳过
			if len(targetArch) > 0 && !utils.Contains(targetArch, goarch) {
				continue
			}

			// 检查组合是否受支持
			if IsSupportedCombination(goos, goarch) {
				buildList = append(buildList, Platform{OS: goos, Arch: goarch})
			}
		}
	}

	return buildList
}
